<?php
/**
 * @version $Id: lookupbl.php 174 2008-02-24 15:56:02Z mikhail.miguel $
 */

class LookupBlacklist
{
    var $_blacklists = array();

    function setBlacklists($blacklists)
    {
        if (is_array($blacklists)) {
            $this->_blacklists = $blacklists;
            return true;
        } else {
            return false;
        }
    }

    function isListed($host)
    {
        foreach ($this->_blacklists as $blacklist) {
            $built_host = $host. '.'. $blacklist;
            $ret = gethostbyname($built_host);
            if($ret != $built_host) {
                return true;
            }
        }
        return false;
    }

    function reverseIP($ip) 
    {        
        return implode('.', array_reverse(explode('.', $ip)));        
    }

    function checkIP($ip)
    {
        $oct = explode('.', $ip);
        if (count($oct) != 4) {
            return false;
        }
        for ($i = 0; $i < 4; $i++) {
            if (!preg_match("/^[0-9]+$/", $oct[$i])) {
                return false;
            }
            if ($oct[$i] < 0 || $oct[$i] > 255) {
                return false;
            }
        }
        return true;
    }

}
?>